class Solution{
    public boolean isBalanced(TreeNode root) {
        if (root==null){
            return true;
        }

        int leftsize=MaxDepth(root.left);
        int rightsize=MaxDepth(root.right);

        return Math.abs(leftsize-rightsize)<2&&isBalanced(root.left)&&isBalanced(root.right);
    }

    public int MaxDepth(TreeNode root){
        if (root == null){
            return 0;
        }

        int leftsize=MaxDepth(root.left);
        int rightsize=MaxDepth(root.right);

        return (leftsize>rightsize)?leftsize+1:rightsize+1;
    }
}

public class Text2 {
}
